package com.open.androidtvwidget;

import java.util.ArrayList;
import java.util.List;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.RecycledViewPool;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DemoRecyclerViewActivity extends Activity {

	RecyclerView mRecyclerView;
	RecycledViewPool mRecycledViewPool;
	private MyAdapter mMyAdapter;
	private List<String> data;
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.demo_recycler_view);
		mContext = getApplicationContext();
		initAllDatas();
		initAllViews();
	}

	private void initAllDatas() {
		data = new ArrayList<String>();
		for (int i = 0; i < 100; i++) {
			data.add("test " + i);
		}
	}

	private void initAllViews() {
		// ImageView imageView1 = (ImageView) findViewById(R.id.imageView1);
		mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
		mRecycledViewPool = mRecyclerView.getRecycledViewPool();
		mRecyclerView.setItemViewCacheSize(10);
		// mRecycledViewPool.setMaxRecycledViews(viewType, max);
		// LinearLayoutManager layout = new LinearLayoutManager(mContext);
		// GridLayoutManager layout = new GridLayoutManager(mContext, 4);
		StaggeredGridLayoutManager layout = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);
		mMyAdapter = new MyAdapter(getApplicationContext(), data);
		mRecyclerView.setLayoutManager(layout);
		mRecyclerView.setItemAnimator(new MyItemAnimator());
		// mRecyclerView.addItemDecoration(decor);
		mRecyclerView.setAdapter(mMyAdapter);
		//
		// Glide.with(this).load("http://h.hiphotos.baidu.com/image/pic/item/4ec2d5628535e5dd2820232370c6a7efce1b623a.jpg")
		// .placeholder(R.drawable.ic_launcher).into(imageView1);
	}

	class MyAdapter extends RecyclerView.Adapter<ViewHolder> {

		private Context mContext;
		private List<String> mData;

		public MyAdapter(Context context, List<String> data) {
			this.mContext = context;
			this.mData = data;
		}

		@Override
		public int getItemCount() {
			return mData.size();
		}
		
		@Override
		public int getItemViewType(int position) {
			//
			return super.getItemViewType(position);
		}
		
		@Override
		public void onBindViewHolder(ViewHolder viewHolder, int i) {
			MyViewHolder myViewHolder = (MyViewHolder) viewHolder;
			myViewHolder.textView.setText(mData.get(i));
		}

		@Override
		public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
			View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
			return new MyViewHolder(v);
		}

		class MyViewHolder extends RecyclerView.ViewHolder {
			
			public TextView textView;
			
			public MyViewHolder(View itemView) {
				super(itemView);
				textView = (TextView) itemView.findViewById(R.id.textView);
			}

		}

	}

	class MyItemAnimator extends RecyclerView.ItemAnimator {

		List<RecyclerView.ViewHolder> mAnimationAddViewHolders = new ArrayList<RecyclerView.ViewHolder>();
		List<RecyclerView.ViewHolder> mAnimationRemoveViewHolders = new ArrayList<RecyclerView.ViewHolder>();

		@Override
		public boolean animateAdd(ViewHolder viewHolder) {
			return mAnimationAddViewHolders.add(viewHolder);
		}

		@Override
		public boolean animateChange(ViewHolder viewHolder, ViewHolder arg1, int arg2, int arg3, int arg4, int arg5) {
			return false;
		}

		@Override
		public boolean animateMove(ViewHolder viewHolder, int arg1, int arg2, int arg3, int arg4) {
			return false;
		}

		@Override
		public boolean animateRemove(ViewHolder viewHolder) {
			return mAnimationRemoveViewHolders.add(viewHolder);
		}

		@Override
		public void endAnimation(ViewHolder viewHolder) {
		}

		@Override
		public void endAnimations() {
		}

		@Override
		public boolean isRunning() {
			return !(mAnimationAddViewHolders.isEmpty() && mAnimationRemoveViewHolders.isEmpty());
		}

		@Override
		public void runPendingAnimations() {
			if (!mAnimationAddViewHolders.isEmpty()) {
				AnimatorSet animator;
				View target;
				for (final RecyclerView.ViewHolder viewHolder : mAnimationAddViewHolders) {
					target = viewHolder.itemView;
					animator = new AnimatorSet();
					animator.playTogether(
							ObjectAnimator.ofFloat(target, "translationX", -target.getMeasuredWidth(), 0.0f),
							ObjectAnimator.ofFloat(target, "alpha", target.getAlpha(), 1.0f));
					animator.setTarget(target);
					animator.setDuration(100);
					animator.addListener(new AnimatorListener() {
						@Override
						public void onAnimationEnd(Animator animation) {
							mAnimationAddViewHolders.remove(viewHolder);
							if (!isRunning()) {
								dispatchAnimationsFinished();
							}
						}

						@Override
						public void onAnimationStart(Animator animation) {
						}

						@Override
						public void onAnimationCancel(Animator animation) {
						}

						@Override
						public void onAnimationRepeat(Animator animation) {
						}
					});
					animator.start();
				}
			}
		}

	}

}
